package com.wlts.wzy.sys_shiro.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlts.wzy.Result.Result;
import com.wlts.wzy.sys_shiro.entity.SysPermission;
import com.wlts.wzy.sys_shiro.entity.SysRole;
import com.wlts.wzy.sys_shiro.entity.SysRolePermission;
import com.wlts.wzy.sys_shiro.service.impl.SysPermissionServiceImpl;
import com.wlts.wzy.sys_shiro.service.impl.SysRolePermissionServiceImpl;
import com.wlts.wzy.sys_shiro.service.impl.SysRoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色权限关系表 前端控制器
 * </p>
 *
 * @author wuzhiyong
 * @since 2019-04-22
 */
@ApiIgnore
@RestController
@RequestMapping("/sys-role-permission")
public class SysRolePermissionController {
    @Autowired
    private SysPermissionServiceImpl permissionService;
    @Autowired
    private SysRolePermissionServiceImpl sysRolePermissionService;
    @Autowired
    private SysRoleServiceImpl sysRoleService;
    /**
     * 添加用户角色关系
     * @param sysRolePermission
     * @return
     */
    @RequestMapping("buildsysRolePermission")
    public Result buildsysRolePermission(@RequestBody SysRolePermission sysRolePermission){
        SysRole sysRole=sysRoleService.getById(sysRolePermission.getRoleId());
        if (sysRole!=null){
            SysPermission sysPermission=permissionService.getById(sysRolePermission.getPermissionId());
            if (sysPermission!=null){
                SysRolePermission sysRolePermission1=sysRolePermissionService.getOne(new QueryWrapper<SysRolePermission>().eq("permission_id",sysRolePermission.getPermissionId()).eq("role_id",sysRolePermission.getRoleId()).eq("del_flag","0"));
                if(sysRolePermission1==null){
                    sysRolePermissionService.save(sysRolePermission);
                      return new Result<>();
                }
            }
        }
        return new Result<>(500,"用户角色关系重复！");
    }

    /**
     *  添加用户角色关系List
     * @param sysRolePermissionList
     * @return
     */
    @RequestMapping("buildsysRolePermissionList")
    public Result buildsysRolePermissionList(@RequestBody List<SysRolePermission> sysRolePermissionList){
        for (SysRolePermission sysRolePermission:sysRolePermissionList
        ) {
            SysRole sysRole=sysRoleService.getById(sysRolePermission.getRoleId());
            if (sysRole!=null){
                SysPermission sysPermission=permissionService.getById(sysRolePermission.getPermissionId());
                if (sysPermission!=null){
                    SysRolePermission sysRolePermission1=sysRolePermissionService.getOne(new QueryWrapper<SysRolePermission>().eq("permission_id",sysRolePermission.getPermissionId()).eq("role_id",sysRolePermission.getRoleId()));
                    if(sysRolePermission1==null){
                        sysRolePermissionService.save(sysRolePermission);
                    }
                }
            }
        }
          return new Result<>();
    }
    /**
     *  删除用户角色关系
     * @param sysRolePermission
     * @return
     */
    @RequestMapping("deleteRolePermission")
    public Result deleteRolePermission(@RequestBody SysRolePermission sysRolePermission) {
        SysRolePermission sysRolePermission1=sysRolePermissionService.getOne(new QueryWrapper<SysRolePermission>().eq("permission_id",sysRolePermission.getPermissionId()).eq("role_id",sysRolePermission.getRoleId()));
        if (sysRolePermission1 != null) {
            sysRolePermissionService.removeById(sysRolePermission1);
              return new Result<>();
        }
        return new Result<>(500,"删除失败！");
    }

    //查看角色拥有的权限
    @RequestMapping("SeeRolePermission")
    public Result SeeRolePermission(@RequestBody SysRolePermission sysRolePermission){
        List<SysRolePermission> sysRolePermissionList=sysRolePermissionService.list(new QueryWrapper<SysRolePermission>().eq("role_id",sysRolePermission.getRoleId()));
        List<SysPermission> permissions = new ArrayList<SysPermission>();
        for (SysRolePermission rolePermission:sysRolePermissionList) {
            SysPermission permission = permissionService.getById(rolePermission.getPermissionId());
            permissions.add(permission);
        }
          return Result.buildResult(permissions);
    }
    //查看权限对应的角色
    @RequestMapping("SeePermissionRole")
    public Result SeePermissionRole(@RequestBody SysRolePermission sysRolePermission){
        List<SysRolePermission> sysRolePermissionList=sysRolePermissionService.list(new QueryWrapper<SysRolePermission>().eq("permission_id",sysRolePermission.getPermissionId()));
        List<SysRole> sysRoleList = new ArrayList<SysRole>();
        for (SysRolePermission rolePermission:sysRolePermissionList) {
            SysRole sysRole = sysRoleService.getById(rolePermission.getRoleId());
            sysRoleList.add(sysRole);
        }
          return Result.buildResult(sysRoleList);
    }

}
